package com.songcy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.ObjectDao;
import com.pro.bean.Users;
import com.pro.bean.Visit_Log;

public class LogServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Users u = (Users)request.getSession().getAttribute("Users");
		int cardid = Integer.parseInt(request.getParameter("cardid"));
		SimpleDateFormat s = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		//Date d = s.format(new Date());
		Visit_Log vl = new Visit_Log();
		vl.setCardId(cardid);
		vl.setUnitId(u.getUnitid());
		vl.setUserId(u.getUserid());
		vl.setVisitTime(new Date((new java.util.Date()).getTime()));
		ObjectDao obdao = new ObjectDao();
		try {
			obdao.add(vl, ObjectDao.NOTINSERTID);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.getRequestDispatcher("showServlet?cardid="
					+request.getParameter("cardid")+"&mode="+request.getParameter("mode")).forward(request, response);
	}

}
